<?php

/**
 * 第三方认证服务
 * @author wyb
 */

namespace Api\Server;

class TrilateralCheckServer {

    public static $_self = null;

    /**
     * 初始化类
     */
    public static function init() {
        return self::$_self == null ? new self() : self::$_self;
    }

    /**
     * 数据加密价签
     */
    public function aign(&$data, $customer_key) {
        $data = $this->_dataSort($data);
        //按字母进行升序排序
        ksort($data);
        $trade_data = array();
        foreach ($data as $key => $value) {
            $trade_data[] = "{$key}={$value}";
        }
        return strtoupper(md5(implode('&', $trade_data) . '&key=' . $customer_key));
    }

    /**
     * 数据过滤以及排序
     */
    private function _dataSort($data) {
        //字符转移
        if (get_magic_quotes_gpc()) {
            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k] = htmlspecialchars_decode(stripslashes($v));
                }
            }
        } else {

            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k] = htmlspecialchars_decode($v);
                }
            }
        }


        //过滤空数据
        foreach ($data as $key => $value) {
            if ($value == '' || $value == null)
                unset($data[$key]);
        }
        return $data;
    }

}
